//
//  ViewController.swift
//  MTBLETools
//
//  Created by 方强 on 16/8/3.
//  Copyright © 2016年 MT. All rights reserved.
//

import UIKit
import MTSDK
import FMDB

class ViewController: UITabBarController {
    private var mGlobalData:StaticDatas!;
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        mGlobalData = StaticDatas.getInstance();
        
        initDB();
        initBLE();
//
//        reciveBroadcast();
    }
    
    // 初始化数据库
    private func initDB(){
        print("初始化数据库");
        let fileURL = try! FileManager.default.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("mydb.sqlite");
        
        guard let database = FMDatabase(path: fileURL.path) else {
            print("unable to create database");
            return;
        }
        mGlobalData.db = database;
        
        UuidAndName.createTable(database: database);  // 创建这2个数据库
        MixDatas.createTable(database: database);
        
        let bindDevices = UuidAndName.findAll(database: database);
        var mixDatas = MixDatas.findAll(database: database);
        
        if(bindDevices?.count == 0){   // 获取存储的UUID
            let wxfd = UuidAndName(name: "微信FD", uuid: "FDA50693-A4E2-4FB1-AFCF-C6EB07647825");
            wxfd.setId(id: UuidAndName.add(database: database, device: wxfd));
            let wxab = UuidAndName(name: "微信AB", uuid: "AB8190D5-D11E-4941-ACC4-42F30510B408");
            wxab.setId(id: UuidAndName.add(database: database, device: wxab));
            
            mGlobalData.uuidList.append(wxfd);
            mGlobalData.uuidList.append(wxab);
        }else{
            mGlobalData.uuidList = bindDevices!;
        }
        
        if(mixDatas == nil){
            mixDatas = MixDatas();
            mixDatas!.id = MixDatas.add(database:database, device:mixDatas!);
        }
        mGlobalData.mixDatas = mixDatas!;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

//    // 蓝牙状态改变
//    public func bleEnableChange(notification:NSNotification){
//        let enble = notification.object as! Bool;
//        
//        if(enble){
//            MTTools.logout(log: "开启蓝牙");
//            
//            //            seriBle.connect("88:C2:55:11:64:1D", callback2: seribleCallback, retryTimes: 3);
//            
//            // "88:C2:55:11:64:1D"  // 不用密码
//            // "88:C2:55:11:67:A2"
////            krjdBle.connect(mac: "88:C2:55:11:67:A2", devicetype: 1, devicepwd: "123456", callback3: krjdCallback, retryTimes: 2);
//        }else{
//            MTTools.logout(log: "关闭蓝牙");
//        }
//    }
//    
//    // 初始化广播
//    private func reciveBroadcast(){
//        let nc = NotificationCenter.default;
//        
//        nc.addObserver(self, selector:#selector(self.bleEnableChange), name: NSNotification.Name(rawValue: MTBLEManager.ENABLEBLE_CHANGE), object: nil);
//    }

    
    // 初始化蓝牙
    private var manager:MTBLEManager!;
//    private var seriBle:MTSeriaBase!;
//    private var seribleCallback:MTSeriaBaseConnectCallback!;
//    private var krjdBle:KRJDBase!;
//    private var krjdCallback = KRJDBaseConnectCallback();
    private func initBLE(){
        manager = MTBLEManager.getInstance();
//        seriBle = MTSeriaBase(manager: manager);
//        
//        seribleCallback = MTSeriaBaseConnectCallback();
//        seribleCallback.onConnect = onConnect;
//        seribleCallback.onDisConnect = onDisConnect;
//        let erro = seriBle.connect("88:C2:55:11:64:1D", callback2: seribleCallback, retryTimes: 3);
//        
//        MTTools.logout("erro->\(erro?.getMsg())");
        
//        krjdBle = KRJDBase(manager: manager);
//        krjdCallback.onConnect = onConnect;
//        krjdCallback.onDisConnect = onDisConnect;
//        krjdCallback.onManualDisConnect = onManualDisConnect;
    }
//    
//    func onConnect(erro: ErroCode?){
//        MTTools.logout(log: "ViewController onConnect->\(erro?.getMsg())");
//    }
//    
//    func onDisConnect(erro: ErroCode?) {
//        MTTools.logout(log: "ViewController onDisConnect->\(erro?.getMsg())");
//    }
//    
//    func onManualDisConnect(erro: ErroCode?) {
//        MTTools.logout(log: "ViewController onManualDisConnect->\(erro?.getMsg())");
//    }
}

